﻿namespace IntellectShop.Web2.Hardware.Controllers
{
	using System;
	using System.Web;
	using System.Web.Mvc;
	using IntellectShop.Common;
	using IntellectShop.Web2.Hardware.ViewModels;

	[IntellectShop.Web2.Hardware.Code.NoCache]
	[IntellectShop.Web2.Hardware.Code.HandleErrorWithELMAH]
	[ValidateInputAttribute(false)]
	public class NewsController : Controller
	{
		const int CountLatestNews = 4;

		public ActionResult ViewLatest()
		{
			var news = Common.News.RetrieveTop(CountLatestNews);
			return View(news);
		}

		//
		// GET: /News/
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Index()
		{
			var news = Common.News.RetrieveAll();
			return View(news);
		}

		//
		// GET: /News/Details/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Details(int newsID)
		{
			var news = Common.News.RetrieveByNewsID(newsID);
			return View(news);
		}

		//
		// GET: /News/Create
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Create()
		{
			return View(new Common.News());
		}

		//
		// POST: /News/Create
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Create(Common.News news)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: News!");

				news.Create(HttpContext.User.Identity.Name);
				return RedirectToAction("Index");
			}
			catch
			{
				return View(new Common.News());
			}
		}

		//
		// GET: /News/Edit/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Edit(int newsID)
		{
			var news = Common.News.RetrieveByNewsID(newsID);
			return View(news);
		}

		//
		// POST: /News/Edit/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Edit(int newsID, FormCollection collection)
		{
			var news = Common.News.RetrieveByNewsID(newsID);
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: News!");

				UpdateModel(news);
				news.Update();
				return Redirect(Url.RouteUrl(new { action = "Index" }) + "#id" + newsID);
			}
			catch
			{
				var newsModel = Common.News.RetrieveByNewsID(newsID);
				return View(newsModel);
			}
		}

		//
		// GET: /News/Delete/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Delete(int newsID)
		{
			var viewModel = new NewsRemoveViewModel { News = Common.News.RetrieveByNewsID(newsID) };
			return View(viewModel);
		}

		//
		// POST: /News/Delete/5
		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Delete(int newsID, FormCollection collection)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: News!");

				Common.News.Delete(newsID);
				return RedirectToAction("Index");
			}
			catch (Exception e)
			{
				var viewModel = new NewsRemoveViewModel
				{
					News = Common.News.RetrieveByNewsID(newsID),
					Message = e.Message
				};
				return View(viewModel);
			}
		}
	}
}
